package com.jin.lazytool.handler;

import com.jin.lazytool.utils.JinUtils;
import org.luaj.vm2.LuaTable;

import java.time.LocalDateTime;

public class LuaLoopHandler extends ILoopHandler{
    public LuaLoopHandler(){}
    private String lua;
    private Integer jumpCount;

    private LuaLoopHandler(String lua, Integer jumpCount){
        this.lua = lua;
        this.jumpCount = jumpCount;
    }

    @Override
    public Integer execute(Integer index) throws InterruptedException {

        LuaTable lua = JinUtils.evalLua(this.lua, 0);
        boolean exeFlag = lua.get("exeFlag").toboolean();

        if(exeFlag){
            Thread.sleep(lua.get("time").tolong());
            return index;
        }else {
            Thread.sleep(time);
            return index + jumpCount;
        }
    }

    @Override
    public ILoopHandler newHandler(Integer count, long timer, LocalDateTime dateTime, String js,Integer jumpCount) {
        return new LuaLoopHandler(js,jumpCount);
    }
}
